﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using VideoReflector.Models;

namespace VideoReflector.Controllers
{
    /// <summary>
    /// Controller tasked with adding clip tag to existing clip tag group
    /// </summary>
    public class AddTagToGroupController : TaskController
    {
        #region Private fields

        private ClipTag _clipTag;
        private int _videoLength;

        #endregion // Private fields

        #region Constructor

        /// <summary>
        /// Constructor for AddTagToGroupController
        /// </summary>
        /// <param name="clipTag"> ClipTag object being created </param>
        /// <param name="videoLength"> Length of video associated with session in seconds </param>
        public AddTagToGroupController(ClipTag clipTag, int videoLength)
        {
            ClipTag = clipTag;
            VideoLength = videoLength;
        }

        #endregion // Constructor

        #region Public properties
        
        /// <summary>
        /// Public accessor of ClipTag object being created
        /// </summary>
        public ClipTag ClipTag
        {
            get
            {
                return _clipTag;
            }
            private set
            {
                _clipTag = value;
                NotifyPropertyChanged("ClipTag");
            }
        }

        /// <summary>
        /// Length of video (in seconds) associated with session clip tag is being added to
        /// </summary>
        public int VideoLength
        {
            get
            {
                return _videoLength;
            }
            private set
            {
                _videoLength = value;
                NotifyPropertyChanged("VideoLength");
            }
        }
                       
        #endregion // Public properties

        #region Commands
        
        #region Ok

        private RelayCommand _okCommand;

        /// <summary>
        /// Invoke Finished event indicating task was completed without cancelling
        /// </summary>
        public ICommand OkCommand
        {
            get
            {
                if (_okCommand == null)
                {
                    _okCommand = new RelayCommand
                    (
                        param => this.Ok(),
                        param => this.CanOk()
                    );
                }
                return _okCommand;
            }
        }

        private void Ok()
        {
            OnFinished(new TaskFinishedEventArgs(false));
        }

        private bool CanOk()
        {
            return ClipTag.StartTime >= 0 && ClipTag.EndTime > ClipTag.StartTime;
        }

        #endregion // Ok  
        
        #region Cancel

        private RelayCommand _cancelCommand;

        /// <summary>
        /// Invoke Finished event indicating task was cancelled
        /// </summary>
        public ICommand CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                {
                    _cancelCommand = new RelayCommand
                    (
                        param => this.Cancel(),
                        param => this.CanCancel()
                    );
                }
                return _cancelCommand;
            }
        }

        private void Cancel()
        {
            OnFinished(new TaskFinishedEventArgs(true));
        }

        private bool CanCancel()
        {
            return true;
        }

        #endregion // Cancel  
                      
        #endregion // Commands
    }
}
